<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\ApiController;
use App\Http\Requests\Permission\PermissionRequest;
use App\Http\Resources\Permission\PermissionResource;
use App\Services\Permission\PermissionService;

class PermissionController extends ApiController
{

    protected $permissionService;

    public function __construct(PermissionService $permissionService)
    {
        $this->permissionService = $permissionService;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $permissions = $this->permissionService->findPermissions();

        return $this->success(PermissionResource::collection($permissions));
    }

    /**
     * Store a newly created resource in storage.
     * @author Jerry
     * @param PermissionRequest $request
     * @return mixed
     */
    public function store(PermissionRequest $request)
    {
        $data = [
            'name'  =>  $request->get('name'),
            'guard_name'    =>  $request->get('guard_name'),
        ];

        try{
            $this->permissionService->insertPermission($data);

            $permissions = $this->permissionService->findPermissions();

            return $this->success(PermissionResource::collection($permissions));
        }catch (\Exception $exception) {
            return $this->failed('添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        try{
            $permission = $this->permissionService->findPermissionById($id);

            return $this->success(new PermissionResource($permission));
        }catch (\Exception $exception) {
            return $this->notFond('请求错误，请检查后重试！');
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        try{
            $permission = $this->permissionService->findPermissionById($id);

            return $this->success(new PermissionResource($permission));
        }catch (\Exception $exception) {
            return $this->notFond('请求错误，请检查后重试！');
        }
    }

    /**
     * Update the specified resource in storage.
     * @author Jerry
     * @param PermissionRequest $request
     * @param $id
     * @return mixed
     */
    public function update(PermissionRequest $request, $id)
    {
        $data = [
            'name'  =>  $request->get('name'),
            'guard_name'    =>  $request->get('guard_name'),
        ];

        try{
            $this->permissionService->updatePermissionById($data, $id);

            $permissions = $this->permissionService->findPermissions();

            return $this->success(PermissionResource::collection($permissions));
        }catch (\Exception $exception) {
            return $this->failed('修改失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        try{
            $this->permissionService->deletePermissionById($id);

            $permissions = $this->permissionService->findPermissions();

            return $this->success(PermissionResource::collection($permissions));
        }catch (\Exception $exception) {
            return $this->failed('删除失败');
        }
    }
}
